package cn.kai.ums.controller;

import cn.kai.ums.mapper.UmsBuildingMapper;
import cn.kai.ums.model.UmsBuilding;
import cn.kai.ums.service.UmsBuildingService;
import cn.kai.ums.tools.JsonData;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("ums/v1/building")
@Api(tags = "教学楼控制类")
public class BuildingController {

    @Autowired
    private UmsBuildingMapper umsBuildingMapper;

    @Autowired
    private UmsBuildingService umsBuildingService;

    @ApiOperation("根据教学楼编号和名称进行模糊查询")
    @GetMapping
    public JsonData getBuildingList(@RequestParam(defaultValue = "1",required = false) Integer pageNum
            ,@RequestParam(defaultValue = "5",required = false) Integer pageSize
            ,@RequestParam(defaultValue = "",value = "likebuilding",required = false) String likebuilding){

        Page<UmsBuilding> umsBuildingPage = umsBuildingMapper.selectPage(new Page<>(pageNum,pageSize)
                , Wrappers.<UmsBuilding>lambdaQuery()
                        .like(UmsBuilding::getBuildingNo,likebuilding)
                        .or()
                        .like(UmsBuilding::getBuildingName,likebuilding));
        return JsonData.buildSuccess(umsBuildingPage);
    }


    @PostMapping
    public JsonData saveBuilding(@RequestBody UmsBuilding umsBuilding){
        return JsonData.buildSuccess(umsBuildingService.save(umsBuilding));
    }

    @PutMapping
    public JsonData updateBuilding(@RequestBody UmsBuilding umsBuilding){
        return  JsonData.buildSuccess(umsBuildingService.updateById(umsBuilding));
    }

    @DeleteMapping
    public  JsonData delBuilding(@RequestParam Integer bid){
        return JsonData.buildSuccess(umsBuildingService.removeById(bid));
    }

    @PostMapping("/delBatchBuilding")
    public JsonData delBatchBuilding(@RequestBody List<Integer> bids){
        return JsonData.buildSuccess(umsBuildingService.removeByIds(bids));
    }
}
